package cn.edu.xmu.orderpayment.payment.Service;

import cn.edu.xmu.oomall.core.util.ReturnObject;
import cn.edu.xmu.orderpayment.payment.Dao.PaymentPatternDao;
import cn.edu.xmu.orderpayment.payment.model.bo.PaymentPattern;
import cn.edu.xmu.orderpayment.payment.model.vo.SimplePaymentPattern;
import cn.edu.xmu.orderpayment.payment.model.po.PaymentPatternPo;
import cn.edu.xmu.privilegegateway.annotation.util.Common;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

import static cn.edu.xmu.privilegegateway.annotation.util.Common.cloneVo;

/**
 * @author Guanlin Wang 22920192204282
 * @date 2021/12/15
 */
@Service
public class PaymentPatternService {
    @Autowired
    private PaymentPatternDao paymentPatternDao;
    /**
     * 获取支付渠道的所有状态
     *
     * @return
     */
    @Transactional(readOnly = true, rollbackFor = Exception.class)
    public ReturnObject getPaymentPatternStates() {
        return paymentPatternDao.getPaymentPatternStates();
    }

    /**
     * 获得当前有效的支付渠道
     *
     * @return
     */
    @Transactional(readOnly = true, rollbackFor = Exception.class)
    public ReturnObject getValidPayPatterns(){
        ReturnObject payPatternsRet = paymentPatternDao.getPayPatternByState(PaymentPattern.State.VALID.getCode());// 通过状态判断是否有效
        List<PaymentPattern> bos = (List<PaymentPattern>) payPatternsRet.getData();
        if(bos == null){
            return payPatternsRet;
        }
        List<SimplePaymentPattern> simplePaymentPatterns = new ArrayList<>();
        for(PaymentPattern bo : bos){
            //进行对象的模型转换
            SimplePaymentPattern simplePaymentPattern = (SimplePaymentPattern)Common.cloneVo(bo, SimplePaymentPattern.class);
            //转换后得结果放到数组里放回
            simplePaymentPatterns.add(simplePaymentPattern);
        }
        return new ReturnObject(simplePaymentPatterns);//返回SimplePaymentPattern类型的
    }

    /**
     * 获得所有支付渠道
     * @return
     */
    @Transactional(readOnly = true, rollbackFor =  Exception.class)
    public ReturnObject getAllPayPatterns(){
        ReturnObject ret = paymentPatternDao.getPayPatternByState(null);// 函数复用, 传入null表示不区分状态
        return ret;
    }

}
